//
//  changePastInfo.m
//  Gradekeeper0.2
//
//  Created by Ben Wu on 11/24/09.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "ChangePastInfo.h"
#import "fileIO.h"
#import "ProfileDetailViewController.h"
#import "Profile.h"
#define NUMBERS @"0123456789"
#define NUMBERSPERIOD	@"0123456789."


@implementation ChangePastInfo

@synthesize segmentControl;

- (void)viewDidLoad {
	//Create a cancel button that lets the user cancel profile creation
	UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" 
																	 style:UIBarButtonItemStyleBordered 
																	target:self 
																	action:@selector(cancelCreate:)]; 
	self.navigationItem.leftBarButtonItem = cancelButton; 
	sem = @"spring";
	[cancelButton release]; 
	
}

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
	[cgpa release];
	[credits release];
	[inputYear release];
	[super dealloc];
}

- (void)cancelCreate:(id)sender {
	
	//If user confirms cancellation, current view will be popped and return to profile manager list (ProfileTableViewController)
	UIAlertView *alert = [[UIAlertView alloc] 
						  initWithTitle:@"Cancel Confirmation" 
						  message:@"Are you sure you would like to cancel editing?" 
						  delegate:self
						  cancelButtonTitle:@"Yes" 
						  otherButtonTitles:nil]; 
	[alert addButtonWithTitle:@"No"];
    [alert show];
	
	[alert release];
	
} //cancelCreate

- (IBAction) btnClickMe_Clicked: (id) sender{
	mycgpa = cgpa.text;
	mycredits = credits.text;
	myYear = inputYear.text;

	if([mycgpa length] == 0 || [mycredits length] == 0 || [myYear length] == 0){
		UIAlertView *alert = [[UIAlertView alloc] 
							  initWithTitle:@"Warning!" 
							  message:@"Please fill in all information."
							  delegate:self
							  cancelButtonTitle:nil
							  otherButtonTitles:nil]; 
		[alert addButtonWithTitle:@"Ok"];
		[alert show];
		[alert release];
	} else if ([mycgpa doubleValue] < 0 || [mycgpa doubleValue] > 5){
		
		UIAlertView *alert = [[UIAlertView alloc] 
							  initWithTitle:@"Invalid input for CGPA" 
							  message:@"Valid CGPA Value: 0.0 ~ 5.0" 
							  delegate:self
							  cancelButtonTitle:nil
							  otherButtonTitles:nil]; 
		[alert addButtonWithTitle:@"Ok"];
		[alert show];
		[alert release];
	} else {
		[self writeToTextFile:self];
		
		UIAlertView *alert = [[UIAlertView alloc] 
							  initWithTitle:@"Success!" 
							  message:@"Information updated" 
							  delegate:self
							  cancelButtonTitle:@"Ok" 
							  otherButtonTitles:nil]; 
		[alert show];
		[alert release];
	}
} //btnClickMe_Clicked


//If the alert is due to invalid username, do nothing
//Otherwise, the alert comes from cancellation request
//If cancellation is confirmed, pop to root view controller
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
    if ([alertView.title isEqualToString:@"Success!"]){
		[[self navigationController] popViewControllerAnimated:YES];
	} else { 
		if (buttonIndex == 0) {
		}
    }
} //alertView

//Takes the username entered and addeds it to profileManager.txt
//and creates a textfile with the same name as the entered username.
-(IBAction) writeToTextFile:(id)sender{
	
	
	NSString *mysem = [sem stringByAppendingString:myYear];
	[mysem retain];
	NSLog(@"mysem = %@", mysem);
	
	Profile *profile = [Profile sharedProfile];
	NSString *myname = [[NSString alloc] initWithFormat: @"%@", [profile getUserName]];

	[profile setCGPA:mycgpa];
	[profile setCreditsUsed:mycredits];
	[profile setSemester:mysem];
	
	[fileIO writeToFile: myname : mycgpa];	
	[fileIO appendToFile: myname : mycredits];
	[fileIO appendToFile: myname : mysem];
		
	
	
		
} //writeToTextFile


// allow only legal input (less than 20 chars, numbers/letters)
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

	textField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
	
	if (textField.tag == 401){
		NSInteger insertDelta = string.length - range.length;
		if (textField.text.length + insertDelta > 5){
			return NO;
		}
		
		NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:NUMBERSPERIOD] invertedSet];
		NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
		return [string isEqualToString:filtered];		
	
		
	} else if (textField.tag == 402){
	
		
		NSInteger insertDelta = string.length - range.length;
		if (textField.text.length + insertDelta > 3){
			return NO;
		}
		
		NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:NUMBERS] invertedSet];
		NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
		return [string isEqualToString:filtered];
		
	} else if (textField.tag == 403){
		
		NSInteger insertDelta = string.length - range.length;
		if (textField.text.length + insertDelta > 4){
			return NO;
		}
		
		NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:NUMBERS] invertedSet];
		NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
		return [string isEqualToString:filtered];
	}
	return YES;
} //textField

//Hides the keyboard after finishing text input (clicking done)
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
	if (textField == cgpa) {
		[cgpa resignFirstResponder];
	}
	else if (textField == credits) {
		[credits resignFirstResponder];
	}
	else if (textField == inputYear) {
		[inputYear resignFirstResponder];
	}
	
	
	return YES;
} //textFieldShouldReturn

- (IBAction) chooseSemester:(id)sender {
	selectedSemester = [self.segmentControl selectedSegmentIndex];
	
	if (selectedSemester==0) {
		sem = @"spring";
	} else if (selectedSemester==1) { 
		sem = @"summer";
	} else if (selectedSemester==2){
		sem = @"fall";
	} else if (selectedSemester==3){
		sem = @"winter";
	}
	
	NSLog(@"%@", sem);
}



@end

